package com.celi.scs.system.task;

import cn.hutool.core.date.DateUtil;
import com.celi.scs.system.entity.LiduEntity;
import com.celi.scs.system.service.LiduService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @project: gaoli-back
 * @title: LiduTask
 * @author: lihq
 * @date: 2024/11/22 13:55
 * @version: v1.0
 * @description: 获取设备粒度任务
 */
//@Component
public class LiduTask {

    @Autowired
    private LiduService liduService;

    @Scheduled(fixedRate = 10000)
    public void init() {
        this.getDeviceLidu();
     //   this.getByte();   测试使用
    }

    private void getByte() {
        byte[] bytes = new byte[] {0, 0, 0, 0, 0, 2, 1, 3, 2, 121, 2, 77, 3, 1, 4, 0, 0, 25, 4, 72, 5, 50, 5, 65, 95, 72, 20, 66, 0, 0, 0, 65, 0, 0, 0, 66, 27, 0, 0, 66, 43, 0, 0, 66, 58, 28, 104, 66, 69, 35, 111, 66, 83, 0, 0, 66, 116, 63, 24, 66, 125, 101, 65, 66, 0, 27, 8, 66, 0, 0, 59, 66, 0, 5, 110, 66, 2, 122, 1, 66, 2, 1, 0, 66, 0, 101, 4, 66, 0, 0, 55, 66, 0, 0, 0, 67, 5, 0, 78, 67, 12, 28, 0, 67, 20, 72, 21, 66, 0, 9, 0, 60, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        this.parseData(bytes);
    }


    public void getDeviceLidu() {
        try {
            // 连接到指定 IP 和端口
            Socket socket = new Socket("192.168.1.10", 1502);

            System.out.println("==========TDT开始读取==========");

            // 构造发送的数据
            byte[] request = {0, 0, 0, 0, 0, 6, 1, 3, 2, 0, 0, 0x7D};

            // 发送数据
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(request);
            outputStream.flush();

            // 接收数据
            InputStream inputStream = socket.getInputStream();
            byte[] buffer = new byte[512];
            int bytesRead = inputStream.read(buffer);

            System.out.println("读取到的数据: ");
            for (int i = 0; i < bytesRead; i++) {
                System.out.printf("0x%02X ", buffer[i]);
            }
            System.out.println();

            System.out.println("==========TDT结束读取==========");

            // 关闭资源
            socket.close();

            // 解析接收到的数据
            parseData(buffer);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void parseData(byte[] buf) {
        LiduEntity entity = new LiduEntity();
        // 数据长度
        int dataLength = ByteBuffer.wrap(new byte[]{0, (byte) 253}).getShort();
        System.out.println("数据长度: " + dataLength);

        // 时间
        long time = ByteBuffer.wrap(new byte[]{buf[10], buf[9], buf[12], buf[11], buf[14], buf[13], buf[16], buf[15]})
                .order(ByteOrder.LITTLE_ENDIAN).getLong();
        System.out.println("时间: " + time);
        entity.setTime(time);

        // 粒子数量
        int particleCount = ByteBuffer.wrap(new byte[]{buf[18], buf[17], buf[20], buf[19]})
                .order(ByteOrder.LITTLE_ENDIAN).getInt();
        System.out.println("粒子数量: " + particleCount);
        entity.setParticleCount(particleCount);

        // Quantity average
        int idx = 21;
        float quantityAverage = ByteBuffer.wrap(new byte[]{buf[idx + 1], buf[idx], buf[idx + 3], buf[idx + 2]})
                .order(ByteOrder.LITTLE_ENDIAN).getFloat();
        System.out.println("Quantity average: " + quantityAverage);
        entity.setQuantityAverage(quantityAverage);

        // Volume average
        idx = 25;
        float volumeAverage = ByteBuffer.wrap(new byte[]{buf[idx + 1], buf[idx], buf[idx + 3], buf[idx + 2]})
                .order(ByteOrder.LITTLE_ENDIAN).getFloat();
        System.out.println("Volume average: " + volumeAverage);
        entity.setVolumeAverage(volumeAverage);

        // 循环解析
        idx = 29;
        int se = 5;
        for (int i = 0; i < 19; i++) {
            float dValue = ByteBuffer.wrap(new byte[]{buf[idx + 1], buf[idx], buf[idx + 3], buf[idx + 2]})
                    .order(ByteOrder.LITTLE_ENDIAN).getFloat();
            System.out.printf("D%d : %.2f%n", se, dValue);
            se += 5;
            if (se == 5) {
                entity.setD5(dValue);
            } else if (se == 10) {
                entity.setD10(dValue);
            } else if (se == 15) {
                entity.setD15(dValue);
            } else if (se == 20) {
                entity.setD20(dValue);
            } else if (se == 25) {
                entity.setD25(dValue);
            } else if (se == 30) {
                entity.setD30(dValue);
            } else if (se == 35) {
                entity.setD35(dValue);
            } else if (se == 40) {
                entity.setD40(dValue);
            } else if (se == 45) {
                entity.setD45(dValue);
            } else if (se == 50) {
                entity.setD50(dValue);
            } else if (se == 55) {
                entity.setD55(dValue);
            } else if (se == 60) {
                entity.setD60(dValue);
            } else if (se == 65) {
                entity.setD65(dValue);
            } else if (se == 70) {
                entity.setD70(dValue);
            } else if (se == 75) {
                entity.setD75(dValue);
            } else if (se == 80) {
                entity.setD80(dValue);
            } else if (se == 85) {
                entity.setD85(dValue);
            } else if (se == 90) {
                entity.setD90(dValue);
            } else if (se == 95) {
                entity.setD95(dValue);
            }
            idx += 4;
        }

        // D43
        float d43 = ByteBuffer.wrap(new byte[]{buf[idx + 1], buf[idx], buf[idx + 3], buf[idx + 2]})
                .order(ByteOrder.LITTLE_ENDIAN).getFloat();
        System.out.println("D43 : " + d43);
        entity.setD43(d43);

        idx += 4;

        // 75μm(-200目)
        float value75um = ByteBuffer.wrap(new byte[]{buf[idx + 1], buf[idx], buf[idx + 3], buf[idx + 2]})
                .order(ByteOrder.LITTLE_ENDIAN).getFloat();
        System.out.println("75μm(-200目): " + value75um);
        entity.setMinus75(value75um);
        idx += 4;

        // 保留位
        float reserved = ByteBuffer.wrap(new byte[]{buf[idx + 1], buf[idx], buf[idx + 3], buf[idx + 2]})
                .order(ByteOrder.LITTLE_ENDIAN).getFloat();
        System.out.println("保留位: " + reserved);
        entity.setReserved(reserved);
        entity.setCreateDate(DateUtil.date());
        entity.setUpdateDate(DateUtil.date());
        entity.setDelFlag(0);
        liduService.insertIntoLidu(entity);
    }

}
